package cn.stylefeng.guns.modular.wms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.wms.entity.ProExp;
import cn.stylefeng.guns.modular.wms.mapper.ProExpMapper;
import cn.stylefeng.guns.modular.wms.model.params.ProExpParam;
import cn.stylefeng.guns.modular.wms.model.result.ProExpResult;
import  cn.stylefeng.guns.modular.wms.service.ProExpService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 项目经验表 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2021-03-23
 */
@Service
public class ProExpServiceImpl extends ServiceImpl<ProExpMapper, ProExp> implements ProExpService {

    @Override
    public void add(ProExpParam param){
        ProExp entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ProExpParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ProExpParam param){
        //判断ID是否存在，如果不存在，则新建
        if (param.getId()!=null){
            ProExp oldEntity = getOldEntity(param);
            ProExp newEntity = getEntity(param);
            ToolUtil.copyProperties(newEntity, oldEntity);
            this.updateById(newEntity);
        }else{
            this.add(param);
        }
    }

    @Override
    public ProExpResult findBySpec(ProExpParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<ProExpResult> findListBySpec(ProExpParam param){
        return baseMapper.customList(param);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ProExpParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ProExpParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ProExp getOldEntity(ProExpParam param) {
        return this.getById(getKey(param));
    }

    private ProExp getEntity(ProExpParam param) {
        ProExp entity = new ProExp();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
